/**
 * 更新场景UI类;
 */
const { ccclass, property } = cc._decorator;
var RequestTypes = {
  CONFIG: 1, //配置;
  FILELIST: 2, //project.menifest; 文件列表;
  RESOURCE: 3, //资源;
  SERVERLIST: 4, //备用;
  VERSION: 5 //version.menifest;
};

@ccclass
export default class hotUpdateUI extends cc.Component {
  @property(cc.ProgressBar)
  progressBar: cc.ProgressBar = null; //进度条;
  // @property(cc.Node)
  // progressShine: cc.Node = null;

  @property(cc.Label)
  lbl_curStep: cc.Label = null; //更新进度文本;

  @property(cc.Label)
  curSize: cc.Label = null; //百分比显示进度;

  @property(cc.Label)
  totalSize: cc.Label = null;

  @property(cc.Node)
  progressRoot: cc.Node = null;

  //////////popup//////////

  // @property(cc.Button)
  // btn_right: cc.Button = null; //右按钮;

  // @property(cc.Button)
  // btn_left: cc.Button = null; //左按钮;

  // @property(cc.Label)
  // lbl_tips: cc.Label = null; //弹窗提示文本;

  // @property(cc.Node)
  // pn_popup: cc.Node = null; //弹窗panel;

  @property(cc.Label)
  version: cc.Label = null; //版本号

  _cancleEvent = null;
  _okEvent = null;
  progressShineStartX: number = null;
  onLoad() {
    console.log("-------------update scene on load!");

    this.init();
  }
  onDestroy() {
    //  cc.vv.update_scene = null;
  }
  init() {
    console.log("update scene init");

    this.progressRoot.active = false;
    this.progressBar.progress = 0.0;
    this.curSize.string = "0%";
    // this.progressShineStartX = this.progressShine.x;
    // this.progressBar.node.active = false;
    //  this.enablePopup(false);
    //click event;
    // var self = this;
    // this.btn_left.node.on(cc.Node.EventType.TOUCH_END, function (event) {
    //   if (self._cancleEvent != null) {
    //     self._cancleEvent();
    //   }
    //   self.enablePopup(false);
    // });
    // this.btn_right.node.on(cc.Node.EventType.TOUCH_END, function (event) {
    //   if (self._okEvent != null) {
    //     self._okEvent();
    //   }
    //   self.enablePopup(false);
    // });
  }

  showVersion(version1, version2) {
    var _version = "";
    if (version1) {
      _version = version1;
    }
    if (version2) {
      _version += " (" + version2 + ")";
    }
    this.version.string = _version;
  }

  /**显示弹窗;
   * des: 描述文本;
   * event: 按钮点击事件;
   */
  // showPopup(cancleEvent, okEvent, des) {
  //   this._cancleEvent = cancleEvent;
  //   this._okEvent = okEvent;

  //   console.log("showPopup = " + des);
  //   //this.enablePopup(true);
  //   //this.lbl_tips.string = "" + des;

  //   // var okNode = this.btn_left.node.getChildByName("lbl_value");
  //   // var lbl_ok = okNode.getComponent(cc.Label);
  //   // lbl_ok.string = "" + left_str;

  //   // var cancelNode = this.btn_right.node.getChildByName("lbl_value");
  //   // var lbl_cancel = cancelNode.getComponent(cc.Label);
  //   // lbl_cancel.string = "" + right_str;
  // }
  /**
   * 显示弹窗与否;
   */
  // enablePopup(flag) {
  //  // this.pn_popup.active = flag;
  // }
  /**
   * 根据热更资源类型刷新进度条和提示;
   */
  updateProgress(type, percent, totalSize) {
    switch (type) {
      case RequestTypes.FILELIST:
      case RequestTypes.CONFIG:
      case RequestTypes.VERSION:
      case RequestTypes.SERVERLIST:
        //this.lbl_curStep.string = cc.vv.GLocalizationDataModel.getStringByKey("Step_Get_Server_Config");
        this.lbl_curStep.string = "获取服务器配置信息...";
        break;
      case RequestTypes.RESOURCE:
        //this.lbl_curStep.string = cc.vv.GLocalizationDataModel.getStringByKey("Step_Get_Server_Resource")
        this.lbl_curStep.string = "" + "正在下载资源...";
        //this.progressBar.node.active = true;
        if (percent) {
          var _percent = percent;
          if (percent >= 0.9999) {
            _percent = 0.9999;
          }

          this.progressBar.progress = _percent; // / 100;
          console.log("_percent  is  ",_percent)
          this.curSize.string = (100 * _percent).toFixed(2) + "%";
          console.log("this.curSize.string   is ***",this.curSize.string)
          this.curSize._forceUpdateRenderData(true);
          //this.progressShine.x = this.progressShineStartX + this.progressBar.node.width * this.progressBar.progress;
        }
        if (totalSize) {
          // this.totalSize.string = totalSize;
        }

        break;
      default:
        break;
    }
  }
  //设置更新信息文本
  setUpdatingStepInfoLabel(stepstr) {
    //var str = cc.vv.GLocalizationDataModel.getStringByKey("TXT_Downloading_File")
    var str = "正在更新文件";
    //正在更新:xxxx;
    this.lbl_curStep.string = str + "" + stepstr;
  }
  //设置更新信息文本
  setStepInfoLabel(fileName, isFinish) {
    //var str = cc.vv.GLocalizationDataModel.getStringByKey("TXT_DownloadFile_OK")
    var str = "更新文件成功:";
    if (isFinish == true) {
      //更新文件成功: xxx;
      this.lbl_curStep.string = str + "(" + fileName + ")";
    }
  }
}
